package cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.text.NumberFormat;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseWebActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.user.MyInvestListAct;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.TransferProInfoResultBean;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.manager.UIManager;
import cn.com.xinjinfu.pdoulcProduct.utils.NewUtil;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;

/**
 * 转让 确认也
 * Created by android-adt on 2016/2/29.
 */
public class TransferProInfoResult extends BaseActivity {
    private CheckBox cb;
    private ImageView iv_selector;
    private RequestQueue requen;
    private String investId;
    private Button bt_zhuanrang;
    private TransferProInfoResultBean mBean;
    private TextView tv_title, tv_time_day, tv_zichan, tv_nian_shouyilv, tv_zhuan_jia, tv_info_xieyi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transfer_pro_info_result);
        TitleManager.showTitle(this, "转让", "");
        ActivityManager.getInstance().addActivity(this);
        Intent intent = getIntent();
        investId = intent.getStringExtra("investId");
        initview();
        requen = Volley.newRequestQueue(this);
        loginRequest("1210", succeedRespon);
    }

    public void initview() {
        RelativeLayout zhuanrang_info = (RelativeLayout) findViewById(R.id.zhuanrang_info);
        zhuanrang_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(TransferProInfoResult.this, R.style.dialog_tran);
                dialog.setContentView(R.layout.dialog_zhaiquan);
                dialog.findViewById(R.id.img_search).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                dialog.setCanceledOnTouchOutside(true);
            }
        });
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_time_day = (TextView) findViewById(R.id.tv_time_day);
        tv_zichan = (TextView) findViewById(R.id.tv_zichan);
        tv_nian_shouyilv = (TextView) findViewById(R.id.tv_nian_shouyilv);
        tv_zhuan_jia = (TextView) findViewById(R.id.tv_zhuan_jia);
        tv_info_xieyi = (TextView) findViewById(R.id.tv_info_xieyi);
        bt_zhuanrang = (Button) findViewById(R.id.bt_zhuanrang);

        iv_selector = (ImageView) findViewById(R.id.iv_selector);
        cb = (CheckBox) findViewById(R.id.cb);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_selector.setImageResource(R.drawable.btn_ok);
                    bt_zhuanrang.setEnabled(true);
                } else {
                    iv_selector.setImageResource(R.drawable.btn_no);
                    bt_zhuanrang.setEnabled(false);
                }
            }
        });
        cb.setChecked(true);

    }

    public void upData() {
        if (mBean != null) {
            tv_title.setText(mBean.getBidTitle());
            tv_time_day.setText(mBean.getDays() + "");
            tv_zichan.setText(TextViewUtil.getSumLength(mBean.getDebtAmount()));
            tv_nian_shouyilv.setText(NumberFormat.getInstance().format(mBean.getApr()) + "%");
            tv_zhuan_jia.setText(TextViewUtil.getSumLength(mBean.getTransferPrice()));
        }
        tv_info_xieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!NewUtil.checkClick(v.getId())) return;
                Intent intent = new Intent(TransferProInfoResult.this, BaseWebActivity.class);
                Bundle bundle = new Bundle();
                String url = DataHandler.WEBURL + "/front/wa/serviceAgreement?pactId=7";
                bundle.putString("url", url);
                bundle.putString("title", "债权转让协议");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        bt_zhuanrang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!NewUtil.checkClick(v.getId())) return;
                if (mBean.getCouponsLimit() > 0) {
                    ToastManager.showShort(TransferProInfoResult.this, "此标的为兜券购买，不能转让");
                } else {
                    UIManager.showSimpleDilog(TransferProInfoResult.this, "确定发起转让" +
                            "\n转让一旦有人购买，\n无法主动取消转让", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            loginRequest("1207", succeedRespon1);
                        }
                    });
                }
            }
        });
    }

    /**
     * 网络请求
     */
    private void loginRequest(String url, Response.Listener<JSONObject> succeed) {
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters(url);
            parameters.put("investId", investId);
            parameters.put("id", user.getCurrUser().getSign());
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeed, null);
            requen.add(request);
        }
    }

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            mBean = JSON.parseObject(jobj.toString(), TransferProInfoResultBean.class);
            upData();
        }
    };
    private Response.Listener<JSONObject> succeedRespon1 = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            if (JSONManager.getError(jobj) == -1) {
                UIManager.showSuccessDilog(TransferProInfoResult.this, R.drawable.img_douhappy, 0, "恭喜您，申请转让成功", "我知道了", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityManager.getInstance().exitActivity();
//                        TransferProInfoResult.this.finish();
                        Intent intent = new Intent(TransferProInfoResult.this, MyInvestListAct.class);
                        Bundle mBundle = new Bundle();
                        mBundle.putInt("xItem", 0);
                        intent.putExtras(mBundle);
                        startActivity(intent);
                    }
                });
            } else {
                ToastManager.showShort(TransferProInfoResult.this, JSONManager.getMsg(jobj));
            }
        }
    };
}
